<?php
/**
 *
 * Author: luffyzhao@vip.126.com
 * DateTime: 2019/3/26 10:50
 */

namespace App\Repositories;


use App\Mail\Customer\UserPasswordMail;;

use App\Models\CustomerUser as Model;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Str;
use LTools\Repositories\RepositoryAbstract;
use Throwable;

class CustomerUser extends RepositoryAbstract
{
    /**
     * @var Model|Model
     */
    protected $model;

    /**
     * RepositoryAbstract constructor.
     *
     * @param Model $model
     */
    public function __construct(Model $model)
    {
        $this->model = $model;
    }

    /**
     * @param array $attributes
     * @param int|null $perPage
     * @param array $columns
     * @param string $pageName
     * @param int|null $page
     * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
     * @author: luffyzhao@vip.126.com
     * @datetime: 2019/4/3 15:40
     */
    public function paginate(array $attributes, int $perPage = null, array $columns = ['*'], $pageName = 'page', int $page = null)
    {
        return $this->model->with(['role'])->where(
            $attributes
        )->paginate($perPage, $columns, $pageName, $page);
    }

    /**
     * @param array $attributes
     * @return bool|Model
     */
    public function create(array $attributes = [])
    {
        $attributes['password'] = Str::random(12);
        $model = parent::create($attributes);

        Mail::to($model->getAttribute('email'))->send(new UserPasswordMail($attributes));

        return $model;
    }

    /**
     * @param $id
     * @param array $values
     * @return bool|Model
     * @throws Throwable
     * @author: luffyzhao@vip.126.com
     * @datetime: 2019/3/29 18:54
     */
    public function status($id, array $values = [])
    {
        /** @var Model $model */
        $model = $this->find($id);
        if ($model->getAttribute('role_id') === null) {
            throw new \Exception("超级账号不能关闭");
        }
        $model->fillable(['status'])->fill($values)->saveOrFail();
        return $model;
    }

    /**
     * @param $id
     * @param array $values
     * @return bool|Model
     * @throws Throwable
     * @author: luffyzhao@vip.126.com
     * @datetime: 2019/3/29 19:43
     */
    public function update($id, array $values)
    {
        /** @var Model $model */
        $model = $this->find($id);
        if (empty($values['password'])) {
            unset($values['password']);
        }else{
            Mail::to($model->getAttribute('email'))->send(new UserPasswordMail($values));
        }
        return $model->fill($values)->saveOrFail();
    }

    /**
     * profileUpdate
     * @param Model $user
     * @param array $values
     * @return bool
     * @throws Throwable
     * @author luffyzhao@vip.126.com
     */
    public function password(Model $user, array $values)
    {
        return $user->fill($values)->saveOrFail();
    }

    /**
     * @param $attributes
     * @return Model|bool
     * @author luffyzhao@vip.126.com
     */
    public function createMasterUser($attributes){
        return $this->create($attributes);
    }

    /**
     * @param Model $model
     * @param $attributes
     * @return bool
     * @throws Throwable
     * @author luffyzhao@vip.126.com
     */
    public function updateMasterUser(Model $model, $attributes){
        $attributes['password'] = Str::random(12);
        Mail::to($model->getAttribute('email'))->send(new UserPasswordMail($attributes));
        return $model->fill($attributes)->saveOrFail();
    }
}
